package com.huawei.hms.framework.network.restclient.hwhttp.trans;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.chromium.net.UploadDataProvider;
import org.chromium.net.UploadDataSink;

public class CronetUploadDataProvider extends UploadDataProvider {
    private static final String TAG = "CronetUploadDataProvide";
    private RequestBody requestBody;

    public CronetUploadDataProvider(RequestBody requestBody) {
        this.requestBody = requestBody;
    }

    public long getLength() throws IOException {
        if (this.requestBody.contentLength() == 0) {
            Logger.w(TAG, "maybe the requestBody's contentLength be not override");
        }
        return this.requestBody.contentLength();
    }

    public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) throws IOException {
        this.requestBody.writeTo(new CronetOutputStream(byteBuffer));
        uploadDataSink.onReadSucceeded(false);
    }

    public void rewind(UploadDataSink uploadDataSink) throws IOException {
        uploadDataSink.onRewindSucceeded();
    }
}
